﻿#include "SnapGroupView.h"
#include "../../Control/Flowlayout.h"
#include "SnapImgItem.h"

#include <QLabel>
#include <QVBoxLayout>
#include <QLayoutItem>
#include <QDebug>
#include <QDateTime>

namespace OfUI {

	SnapGroupView::SnapGroupView(QWidget *parent)
		: QWidget(parent)
		, m_pTitleLabel(nullptr)
		, m_pFlowLayout(nullptr)
		, m_eGroupType(GroupType::DAY)
	{
		initForm();
	}

	SnapGroupView::~SnapGroupView()
	{
	}

	void SnapGroupView::setGroupType(const GroupType& eType)
	{
		m_eGroupType = eType;
	}

	GroupType SnapGroupView::getGroupType() const
	{
		return m_eGroupType;
	}

	void SnapGroupView::setGroup(const SnapDataGroup& group, const QString& title)
	{
		m_group = group;
		m_pTitleLabel->setText(title);
		qSort(m_group.m_dataList.begin(), m_group.m_dataList.end(), sortSnapDataByTime);

		for (int i = 0; i < m_pFlowLayout->count() && i < m_group.m_dataList.size(); i++)
		{
			SnapImgItem* pImgItem = static_cast<SnapImgItem*>(m_pFlowLayout->itemAt(i)->widget());
			if (pImgItem)
			{
				pImgItem->setData(m_group.m_dataList.at(i));
			}
		}
	}

	SnapDataGroup SnapGroupView::getGroup()
	{
		return m_group;
	}

	int SnapGroupView::getCount() const
	{
		return m_pFlowLayout->count();
	}

	SnapImgItem* SnapGroupView::findItemById(const int& nId)
	{
		SnapImgItem* pFindItem = nullptr;
		for (int i = 0; i < m_pFlowLayout->count(); i++)
		{
			SnapImgItem* pItem = static_cast<SnapImgItem*>(m_pFlowLayout->itemAt(i)->widget());
			if (pItem && (nId == pItem->getData().m_nId))
			{
				pFindItem = pItem;
				break;
			}
		}
		return pFindItem;
	}

	void SnapGroupView::addItem(SnapImgItem* pItem)
	{
		m_pFlowLayout->addWidget(pItem);
		adjustSize();
	}

	SnapImgItem* SnapGroupView::takeItemByIndex(int nIndex)
	{
		SnapImgItem* pDelItem = nullptr;

		if (nIndex <= m_pFlowLayout->count() - 1)
		{
			pDelItem = dynamic_cast<SnapImgItem*>(m_pFlowLayout->itemAt(nIndex)->widget());
			QLayoutItem* pChild = m_pFlowLayout->takeAt(nIndex);

			if (pChild)
			{
				if (pChild->widget())
				{
					pChild->widget()->setParent(nullptr);
				}
				delete pChild;
			}
		}
		return pDelItem;
	}

	SnapImgItem* SnapGroupView::takeItemById(const int& nId)
	{
		SnapImgItem* pDelItem = nullptr;

		for (int i = 0; i < m_pFlowLayout->count(); i++)
		{
			SnapImgItem* pItem = dynamic_cast<SnapImgItem*>(m_pFlowLayout->itemAt(i)->widget());
			if (pItem && (nId == pItem->getData().m_nId))
			{
				pDelItem = pItem;
				QLayoutItem* pChild = m_pFlowLayout->takeAt(i);

				if (pChild)
				{
					if (pChild->widget())
					{
						pChild->widget()->setParent(nullptr);
					}
					delete pChild;
				}
				break;
			}
		}
		return pDelItem;
	}

	void SnapGroupView::initForm()
	{
		this->setAttribute(Qt::WA_StyledBackground);

		m_pTitleLabel = new QLabel;
		m_pTitleLabel->setStyleSheet("QLabel{min-height: 30px; max-height: 30px; font: 14px \"Microsoft YaHei\"; font-weight: bold;}");

		m_pFlowLayout = new FlowLayout(0, 8, 10);

		QFrame* pHLineFrame = new QFrame;
		pHLineFrame->setStyleSheet("QFrame{min-height: 1px; max-height: 1px; background-color: rgb(198,198,198);}");

		QVBoxLayout* pMainLayout = new QVBoxLayout(this);
		pMainLayout->setMargin(0);
		pMainLayout->setSpacing(5);
		pMainLayout->addWidget(m_pTitleLabel);
		pMainLayout->addLayout(m_pFlowLayout, 1);
		pMainLayout->addWidget(pHLineFrame);
	}

}
